﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Douban.Modules.Conversation.Views;
using Douban.Api;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Presentation.Events;
using Microsoft.Practices.Unity;
using Douban.Api.User;
using System.Windows;

namespace Douban.Modules.Conversation.ViewModels
{
    public class ConversationShellViewModel : IConversationShellViewModel
    {
        private readonly IUnityContainer _container;
        private readonly DoubanService _doubanService;
        private ICommand _closeCommand;
        private ICommand _minimizeCommand;

        public ConversationShellViewModel(IConversationShellView view, IUnityContainer container, DoubanService doubanService)
        {
            _container = container;
            _doubanService = doubanService;
            View = view;
            View.Context = this;
        }

        #region IConversationViewModel Members

        public IConversationShellView View { get; set; }

        public string ContactId { get; set; }

        public IContactInfoViewModel ContactInfoViewModel { get; set; }

        public IConversationWorkSpaceViewModel ConversationWorkSpaceViewModel { get; set; }

        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                    _closeCommand = new DelegateCommand<object>(obj => CloseConversation());

                return _closeCommand;
            }
        }

        public ICommand MinimizeCommand
        {
            get
            {
                if (_minimizeCommand == null)
                    _minimizeCommand = new DelegateCommand<object>(obj => View.MinimizeView());

                return _minimizeCommand;
            }
        }

        public void Run()
        {
            InitializeContactInfoViewModel(ContactId);
            InitializeConversationWorkSpaceViewModel(ContactId);
            View.ShowView();
        }

        public event RemoveEventHandler RemoveConversationFromCollection;

        #endregion

        private void CloseConversation()
        {
            OnRemoveConversationFromCollection();

            View.CloseView();
        }

        private void OnRemoveConversationFromCollection()
        {
            if (RemoveConversationFromCollection != null)
                RemoveConversationFromCollection(this, new RemoveEventArgs(ContactId));
        }

        private void InitializeContactInfoViewModel(string contactId)
        {
            ContactInfoViewModel = _container.Resolve<IContactInfoViewModel>();
            ContactInfoViewModel.Contact = _doubanService.UserService.GetUserDetail(contactId);
        }

        private void InitializeConversationWorkSpaceViewModel(string contactId)
        {
            ConversationWorkSpaceViewModel = _container.Resolve<IConversationWorkSpaceViewModel>();
            ConversationWorkSpaceViewModel.User = _doubanService.UserService.GetCurrentUser();
            ConversationWorkSpaceViewModel.Contact = _doubanService.UserService.GetUserDetail(contactId);
            ConversationWorkSpaceViewModel.ChangeConversationShellToShowOrCloseChatLog += ChangeConversationShellToShowOrCloseChatLog;
            ConversationWorkSpaceViewModel.Run();
        }

        private void ChangeConversationShellToShowOrCloseChatLog(object sender, EventArgs e)
        {
            IChatLogViewModel chatLogViewModel = ConversationWorkSpaceViewModel.ChatLogViewModel;
            switch (chatLogViewModel.View.Visible)
            {
                case Visibility.Collapsed: 
                    chatLogViewModel.View.Visible = Visibility.Visible;
                    View.ShellWidth += 300;
                    chatLogViewModel.Run();
                    break;
                case Visibility.Visible:
                    chatLogViewModel.View.Visible = Visibility.Collapsed;
                    View.ShellWidth -= 300;
                    break;
                default:
                    break;
            }
        }
    }

    public delegate void RemoveEventHandler(object sender, RemoveEventArgs e);

    public class RemoveEventArgs : EventArgs
    {
        public string ContactId { get; private set; }

        public RemoveEventArgs(string contactId)
        {
            ContactId = contactId;
        }
    }
}
